home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / N_NODE.H < prev    next >
C/C++ Source or Header  |  1992-09-28  |  9KB  |  218 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 07/05/89 -- Initial design
  13. // Updated: MBN 08/10/89 -- Initial implementation
  14. // Updated: MBN 08/11/89 -- Inherit from Generic
  15. // Updated: MBN 09/19/89 -- Added conditional exception handling
  16. // Updated: MBN 12/21/89 -- Added optional argument to set_compare method
  17. // Updated: MBN 02/27/90 -- Added operator= for pointer argument
  18. // Updated: MJF 03/12/90 -- Added group names to RAISE
  19. // Updated: VDN 02/21/92 -- New lite version
  20. // Updated: JAM 08/19/92 -- modernized template syntax, remove macro hacks
  21. // Updated: JAM 09/28/92 -- added ItemType and max_children members for
  22. //                          new N_Tree convention
  23. //
  24. // The N_Node<Type,nchild>  class implements parameterized   nodes of a  static
  25. // size  for N-ary trees.   This node class  is parameterized for both the type
  26. // and some "N", the number of  subtrees each node  may have.  The constructors
  27. // for  the  N_Node<Type,nchild> class are  declared  in the public  section to
  28. // allow the user to create nodes and control  the building and structure of an
  29. // N-ary  tree where  the   ordering can have  a specific   meaning, as with an
  30. // expression tree.
  31. //
  32. // The  private data section contains  just two slots.  The  first is  a static
  33. // sized vector of "N" pointers to N_Node objects, one for  each subtree.   The
  34. // second is  a data  slot  of type Type  to hold the  value  of the data item.
  35. // There are three  public constructors for the  N-Node class.  The first takes
  36. // no arguments and initializes the pointer and data slots to NULL.  The second
  37. // takes an argument of type Type and initializes the data  slot to that value.
  38. // The  third takes a  reference to  another  N-Node object  and duplicates its
  39. // values.
  40. //
  41. // Methods are provided to set and get the node data value, determine if a node
  42. // is a leaf or the root of some subtree,  and implement member-wise assignment
  43. // from one  N-Node to another via  the overloaded operator=. In  addition, the
  44. // brackets operator is overloaded to  provided  a mechanism to efficiently get
  45. // and set individual  subtree pointers in  a specific node,  thus allowing the
  46. // user to   control  the  orgranization  and  structure   of a tree.  Finally,
  47. // insertion, and removal  methods to allow the  user to control  placement and
  48. // ordering of sub-trees of a node are available.
  49. //
  50.  
  51. #ifndef N_NODEH                    // If no definition for class
  52. #define N_NODEH
  53.  
  54. #ifndef MISCELANEOUSH        // If we have not included this file,
  55. #include <misc.h>        // include miscelaneous useful definitions.
  56. #endif
  57.  
  58. template <class Node>
  59. class CoolN_Tree;    // Forward reference class
  60.  
  61. template <class Type, int nchild>
  62. class CoolN_Node {
  63. public:
  64.   typedef Boolean (*Compare)(const Type&, const Type&);
  65.   typedef CoolN_Node<Type,nchild>* CoolN_Node_p; //Pointer to class
  66.  
  67.   typedef Type ItemType;
  68.   static int max_children() { return nchild; }
  69.  
  70.   CoolN_Node();            // Simple constructor
  71.   CoolN_Node(const Type&);    // constructor with data value
  72.   CoolN_Node(const CoolN_Node<Type,nchild>&); // Copy constructor
  73.   ~CoolN_Node();            // Destructor
  74.  
  75.   inline void set (const Type&);        // Set node data to value
  76.   inline Type& get ();                // Get node data value
  77.   Boolean is_leaf () const;            // TRUE if node has no children
  78.   inline CoolN_Node_p& operator[] (int); // Set/Get pointers
  79.   inline int num_subtrees () const;         // Number of subtree slots
  80.   
  81.   inline void set_compare (Compare = NULL); // Set compare
  82.   inline Boolean operator== (const Type&) const;     // Overload operator==
  83.   inline Boolean operator!= (const Type&) const;     // Overload operator!=
  84.   inline Boolean operator< (const Type&) const;         // Overload operator<
  85.   inline Boolean operator> (const Type&) const;         // Overload operator>
  86.   inline Boolean operator<= (const Type&) const;     // Overload operator<=
  87.   inline Boolean operator>= (const Type&) const;     // Overload operator>=
  88.  
  89.   CoolN_Node<Type,nchild>& operator= (const CoolN_Node<Type,nchild>&); // Assignment
  90.   Boolean insert_before (CoolN_Node<Type,nchild>&, int); // Insert subtree
  91.   Boolean insert_after (CoolN_Node<Type,nchild>&, int);     // Insert subtree
  92.  
  93. private:
  94. public: //## when friend below works with BC++, delete this line
  95. //##  CoolN_Node_p sub_trees[nchild]; // Vector of subtree pointers
  96.   CoolN_Node<Type,nchild>* sub_trees[nchild]; // Vector of subtree pointers
  97.   Type data;                    // Slot to hold data value
  98.   static Compare compare_s;    // Compare function for class
  99.  
  100.   CoolN_Node<Type,nchild>* copy_nodes(const CoolN_Node<Type,nchild>*) const; // Deep copy
  101.   void index_error (const char* fcn, int i);    // Raise exception
  102.  
  103. //##  friend class CoolN_Tree<CoolN_Node<Type,nchild> >;  // Friend class to access data
  104.   friend int default_CoolN_Node_compare (const Type&, const Type&);
  105. };
  106.  
  107.  
  108.  
  109. // num_subtrees -- Returns number of slots available for subtrees in node
  110. // Input:          None
  111. // Output:         int length of the allocated array for Nodes
  112.  
  113. template <class Type, int nchild>
  114. inline int CoolN_Node<Type,nchild>::num_subtrees () const {
  115.   return nchild;
  116. }
  117.  
  118. // set -- Set value of data slot in node
  119. // Input: Reference to data slot value
  120. // Output: None
  121.  
  122. template <class Type, int nchild> 
  123. inline void CoolN_Node<Type,nchild>::set (const Type& value) {
  124.   this->data = value;                // Set data slot value
  125. }
  126.  
  127.  
  128. // get -- Get value of data slot in node
  129. // Input: None
  130. // Output: Reference to data slot value
  131.  
  132. template <class Type, int nchild> 
  133. inline Type& CoolN_Node<Type,nchild>::get () {
  134.   return this->data;                // Return data slot value
  135. }
  136.  
  137.  
  138. // set_compare -- Specify the comparison function to be used in logical tests
  139. //                of node data values
  140. // Input:         Pointer to a compare function
  141. // Output:        None
  142.  
  143. template <class Type, int nchild> 
  144. inline void CoolN_Node<Type,nchild>::set_compare (/*Compare##*/Boolean (*c)(const Type&, const Type&)) {
  145.   if (c == NULL)
  146.     this->compare_s = &default_CoolN_Node_compare; // Default equality
  147.   else
  148.     this->compare_s = c;            // Else use one provided
  149. }
  150.  
  151.  
  152. // operator== -- Overload the equality operator to use the compare function
  153. // Input:        constant reference to Type value
  154. // Output:       TRUE/FALSE
  155.  
  156. template <class Type, int nchild> 
  157. inline Boolean CoolN_Node<Type,nchild>::operator== (const Type& value) const {
  158.   return ((((*this->compare_s)(this->data,value)) == 0) ? TRUE : FALSE);
  159. }
  160.  
  161.  
  162. // operator!= -- Overload the inequality operator to use the compare function
  163. // Input:        constant reference to Type value
  164. // Output:       TRUE/FALSE
  165.  
  166. template <class Type, int nchild> 
  167. inline Boolean CoolN_Node<Type,nchild>::operator!= (const Type& value) const {
  168.   return ((((*this->compare_s)(this->data,value)) == 0) ? FALSE : TRUE);
  169. }
  170.  
  171.  
  172. // operator< -- Overload the less than operator to use the compare function
  173. // Input:       constant reference to Type value
  174. // Output:      TRUE/FALSE
  175.  
  176. template <class Type, int nchild> 
  177. inline Boolean CoolN_Node<Type,nchild>::operator< (const Type& value) const {
  178.   return ((((*this->compare_s)(this->data,value)) < 0) ? TRUE : FALSE);
  179. }
  180.  
  181.  
  182. // operator> -- Overload the greater than operator to use the compare function
  183. // Input:       constant reference to Type value
  184. // Output:      TRUE/FALSE
  185.  
  186. template <class Type, int nchild> 
  187. inline Boolean CoolN_Node<Type,nchild>::operator> (const Type& value) const {
  188.   return ((((*this->compare_s)(this->data,value)) > 0) ? TRUE : FALSE);
  189. }
  190.  
  191.  
  192. // operator<= -- Overload the less than or equal operator to use the compare
  193. //               function
  194. // Input:        constant reference to Type value
  195. // Output:       TRUE/FALSE
  196.  
  197. template <class Type, int nchild> 
  198. inline Boolean CoolN_Node<Type,nchild>::operator<= (const Type& value) const {
  199.   return ((((*this->compare_s)(this->data,value)) > 0) ? FALSE : TRUE);
  200. }
  201.  
  202.  
  203. // operator>= -- Overload the greater than or equal operator to use the compare
  204. //               function
  205. // Input:        constant reference to Type value
  206. // Output:       TRUE/FALSE
  207.  
  208. template <class Type, int nchild> 
  209. inline Boolean CoolN_Node<Type,nchild>::operator>= (const Type& value) const {
  210.   return ((((*this->compare_s)(this->data,value)) < 0) ? FALSE : TRUE);
  211. }
  212.  
  213.  
  214.  
  215. #endif                        // End N_NODEH #if
  216.  
  217.  
  218.